package com.list88zhu.android.sunflower.api;

import android.text.TextUtils;

import com.list88zhu.android.sunflower.api.impl.adapter.DefaultAdapter;
import com.list88zhu.android.sunflower.api.impl.config.DefaultConfig;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.plugins.RxJavaPlugins;
import okhttp3.ResponseBody;

/**
 * <pre><code>
 *
 *  </code></pre>
 *
 * @author zhuyingming@qmtv.com
 * @date 2018/8/31
 */
public class ApiClient {

    private static boolean isDebug;
    private static ApiClient apiClient;
    private List<ApiErrorHandler> errorHandlers;

    private ApiClient() {
        errorHandlers = new ArrayList<>();
        RxJavaPlugins.setErrorHandler(throwable -> {
            if (errorHandlers != null && errorHandlers.size() > 0) {
                for (ApiErrorHandler errorHandler : errorHandlers) {
                    errorHandler.handleError(throwable);
                }
            }
        });
    }

    public static ApiClient getApiClient() {
        if (apiClient == null) {
            synchronized (ApiClient.class) {
                if (apiClient == null) {
                    apiClient = new ApiClient();
                }
            }
        }
        return apiClient;
    }

    public ApiClient initApi(Api.ConfigParams params){
        isDebug = params.isDebug;
        Api.init(params);
        return getApiClient();
    }

    public static <T> T create(Class<T> service) {
        if (service == null) {
            throw new IllegalArgumentException("service can't be null.");
        }
        String configName = service.getCanonicalName();
        if(configName != null) {
            return getProxy(configName).create(service);
        }
        return null;
    }

    private static ApiProxy getProxy(String config) {
        if(TextUtils.isEmpty(config)) {
            return Api.provide(DefaultConfig.class);
        }else {
            return Api.provide(DefaultConfig.class);
        }
    }

    public static RequestBuilder<ResponseBody> get(String url) {
        return get(url, DefaultConfig.class);
    }

    public static RequestBuilder<ResponseBody> get(String url, Class<? extends Api.Config> config) {
        ApiProxy apiProxy = Api.provide(config != null ? config : DefaultConfig.class);
        return apiProxy.createBuilder(new DefaultAdapter(apiProxy)).url(url).method("get");
    }

    public static RequestBuilder<ResponseBody> post(String url) {
        return post(url, DefaultConfig.class);
    }

    public static RequestBuilder<ResponseBody> post(String url, Class<? extends Api.Config> config) {
        ApiProxy apiProxy = Api.provide(config != null ? config : DefaultConfig.class);
        return apiProxy.createBuilder(new DefaultAdapter(apiProxy)).url(url).method("post");
    }

    public ApiClient addErrorHandler(ApiErrorHandler errorHandler) {
        apiClient.errorHandlers.add(errorHandler);
        return apiClient;
    }



    public interface ApiErrorHandler {

        void handleError(Throwable throwable);
    }

}
